package br.ufc.si.managedBean;

import java.io.IOException;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import modelo.Imovel;
import modelo.Servico;
import modelo.Usuario;
import dao.ServicoDao;

@ManagedBean
public class CadastroServicoBean {
	private Servico servico = new Servico();

	private ServicoDao servicoDao = new ServicoDao();
	
	private String msg;
	
	public boolean getMostraTabela(){
		return servicoDao.getLista().size() > 0;
	}
	
	public String cadastrar() throws IOException, ClassNotFoundException{
		boolean cadastro = servicoDao.cadastrar(servico);
		if(cadastro==true){
	
			servico = new Servico();
	
			msg = "Serviço cadastrado com sucesso";
			
			return "/admin/index_admin";
			
		}else
		
			msg = "Serviço já cadastrado!";
		
			return "/locador/indexLocador?faces-redirect=true";
		
		
		}
	
	
	
	
	
public String atualizar() throws ClassNotFoundException{
		
		boolean atualizacao = servicoDao.atualizar(servico);
		if(atualizacao==true){
	
			msg = "Serviço atualizado";
			
			return "/admin/index_admin";
			
		}else
		
			return "/admin/index_admin";
		
	}

	
	public String visualizar(){
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		
		try {
			servico = servicoDao.buscar(Integer.parseInt(request.getParameter("id")));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return "/admin/servicos/vizualizar_servico";
	}
	
	
	
	public String editar(){
		
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		
		try {
			servico = servicoDao.buscar(Integer.parseInt(request.getParameter("id")));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		return "/admin/servicos/editar_servico";
	}
	
		public String excluir(){
		
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		
		servicoDao.excluir(Integer.parseInt(request.getParameter("id")));
		
		return "/admin/index_admin?faces-redirect=true";
	}
	
		public String paginaCadastroServico(){
			
		    return "/admin/servicos/cadastrar_servicos?faces-redirect=true";
		}
	
	public String cancelar() {
		return "/admin/index_admin";
	}
	public Servico getServico() {
		return servico;
	}
	public void setServico(Servico servico) {
		this.servico = servico;
	}
	public ServicoDao getServicoDao() {
		return servicoDao;
	}
	public void setServicoDao(ServicoDao servicoDao) {
		this.servicoDao = servicoDao;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}

}
